import javax.security.auth.login.CredentialException;
import javax.swing.tree.TreeNode;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-02-23
 * Time: 19:15
 */
public class BinarySearchTree {

     static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode (int val) {
            this.val = val;
        }
    }
    TreeNode root;

    public boolean search (int val) {
         if (root == null) return false;
         TreeNode cur = root;
         while (cur != null) {
             if (cur.val > val) {
                 cur = cur.left;
             } else if (cur.val < val) {
                 cur = cur.right;
             } else {
                 return true;
             }
         }
         return false;
    }

    public void put (int val) {
        TreeNode node = new TreeNode(val);
        if (root == null) root = node;
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur != null ) {
                if (cur.val > val) {
                    parent = cur;
                    cur = cur.left;
                } else if (cur.val < val) {
                    parent = cur;
                    cur = cur.right;
                } else {
                    return;
            }
        }
        if (parent.val > val) {
            parent.left = node;
        } else {
            parent.right = node;
        }
    }

    public boolean remove (int val) {
        if (root == null) return false;
        TreeNode cur = root;
        TreeNode parent = cur;

        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else {
                break;
            }
        }

        if (cur == null) {
            return false;
        }

        //左右树都为空
        if (cur.left == null && cur.right == null) {
            if (root == cur) {
                cur = null;
            }
            if (parent.right == cur) {
                parent.right = null;
            } else {
                parent.left = null;
            }
            return true;
        }
        //删除位置左树为空
        else if (cur.left == null) {
            if (cur == root) {
                cur = cur.right;
            }
            if (parent.right == cur) {
                parent.right = cur.right;
            } else {
                parent.left = cur.right;
            }
            return true;
        }
        //删除位置右树为空
        else if (cur.right == null) {
            if (cur == root) {
                cur = cur.left;
            }
            if (parent.right == cur) {
                parent.right = cur.left;
            } else {
                parent.left = cur.left;
            }
            return true;
        }
        //删除位置左右树都不为空
        else {
            removeNode(cur, val);
            return true;
        }
    }
    private void removeNode (TreeNode cur,int val) {
        TreeNode targetParent = cur;

        //向左找替罪羊
        TreeNode target = cur.right;

        while (target.left != null) {
            targetParent = target;
            target = target.left;
        }
        if (target == targetParent.left) {
            cur.val = target.val;
            targetParent.left = target.right;
        }
        cur.val = target.val;
        targetParent.right = target.right;
    }
}
